package actions;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Vector;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.VenueModel;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.LabelValueBean;

import db.DBException;
import db.DBModel;
import forms.EventFormInsertUpdate;

public class CreateEventAction extends Action{
	
	public ActionForward execute(ActionMapping mapping, ActionForm form,HttpServletRequest request,HttpServletResponse response){
	try{
		DBModel db = new DBModel();
		
		EventFormInsertUpdate f = (EventFormInsertUpdate) form;
		
		//Add venue list ass collection (because of struts)
		ArrayList<VenueModel> venues = db.getAllVenues();
		f.setVenueList(getVenueCollection(venues));
		
		db.commit();
		db.close();

		return mapping.findForward("success");
		
		}catch(Exception e) {	
			request.setAttribute("error", e.toString());
			e.printStackTrace();
			return mapping.findForward("fail");
		}
	}
	
	private Collection getVenueCollection(ArrayList<VenueModel> venues){
		Collection venueList=new Vector(venues.size());
		//Adds the already selected venue first
		for(int i=0;i<venues.size();i++){
				venueList.add( new LabelValueBean(venues.get(i).getName(),String.valueOf(venues.get(i).getId())) );
		}
		return venueList;
	}
}
